/**
 * 
 */
package sqlToGUI.charts;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;

import javax.swing.JPanel;

import org.jfree.data.general.DefaultValueDataset;

import sqlToGUI.charts.helperUtilities.TelemetryDataEntry;

/**
 * @author Maciej
 *
 */
@SuppressWarnings("serial")
public class SolarArrayPanel extends JPanel implements GUIDisplay {

	private DefaultValueDataset[] buffer;
	private final double[] THRESHOLDS = {0, 1, 2, 3};
	
	public SolarArrayPanel() {
		super();
		this.buffer = new DefaultValueDataset[11];
	}
	
	/**Create the display chart */
	public void paintComponent(Graphics g) {
		int height = this.getHeight();
		int width = this.getWidth();
		int panelX[] = new int[11];
		int panelY[] = new int[11];
		int panelWidth[] = new int[11];
		int panelHeight[] = new int[11];
		
		panelX[0] = 0;	
		panelY[0] = 0;	
		panelWidth[0] = (width / 5) - (width / 200);
		panelHeight[0] = (height / 3) - (height / 200);
		
		panelX[1] = (width / 5);	
		panelY[1] = panelY[0];	
		panelWidth[1] = panelWidth[0];
		panelHeight[1] = panelHeight[0];
		
		panelX[2] = (width / 5) * 2;	
		panelY[2] = panelY[0];	
		panelWidth[2] = panelWidth[0];
		panelHeight[2] = panelHeight[0];
		
		panelX[3] = (width / 5) * 3;	
		panelY[3] = panelY[0];	
		panelWidth[3] = panelWidth[0];
		panelHeight[3] = panelHeight[0];
		
		panelX[4] = (width / 5) * 4;	
		panelY[4] = panelY[0];	
		panelWidth[4] = panelWidth[0];
		panelHeight[4] = panelHeight[0];
		
		panelX[5] = panelX[0];	
		panelY[5] = (height / 3);
		panelWidth[5] = (width / 3);
		panelHeight[5] = panelHeight[0];		
		
		panelX[6] = (width / 3) * 2;	
		panelY[6] = panelY[5];	
		panelWidth[6] = panelWidth[5] - (width/200);
		panelHeight[6] = panelHeight[5];	
		
		panelX[7] = panelX[0];	
		panelY[7] = (height / 3) * 2;	
		panelWidth[7] = (width / 4) - (width / 200);
		panelHeight[7] = panelHeight[0];
		
		panelX[8] = (width / 4);	
		panelY[8] = panelY[7];	
		panelWidth[8] = panelWidth[7];
		panelHeight[8] = panelHeight[7];
		
		panelX[9] = (width / 2);	
		panelY[9] = panelY[7];
		panelWidth[9] = panelWidth[7];
		panelHeight[9] = panelHeight[7];
		
		panelX[10] = (width / 4) * 3;
		panelY[10] = panelY[7];
		panelWidth[10] = panelWidth[7];
		panelHeight[10] = panelHeight[7];
		
		for(int i = 0; i < 11; i++) {
			g.setColor(pickColor(i));
			g.fillRect(panelX[i], panelY[i], panelWidth[i], panelHeight[i]);
			g.setColor(Color.black);
			g.drawRect(panelX[i], panelY[i], panelWidth[i], panelHeight[i]);
		}
	}
	
	/**Moved the frame for which the data will be displayed on the chart */
	@Override
	public void addToBuffer(int index, double value) {
		buffer[index].setValue(value);
		this.repaint();
	}


	/**Returns the chart that plots the data */
	@Override
	public Container getChart() {
		return this;
	}
	
	/**Helper method for setting the color according to the data in the buffer*/
	private Color pickColor(int index) {
		/*double value = buffer[index].getValue().doubleValue();
		
		if (value <= THRESHOLDS[0])
			return Color.black;
		else if (value <= THRESHOLDS[1] && value > THRESHOLDS[0])
			return Color.red;
		else if (value <= THRESHOLDS[2] && value > THRESHOLDS[1])
			return Color.orange;
		else if (value <= THRESHOLDS[3] && value > THRESHOLDS[2])
			return Color.yellow;
		else if (value > THRESHOLDS[3])
			return Color.green;
		else return null; //Uh oh...*/
		return Color.blue;
		
	}
	
	public GUIDisplay registerLog(LogPanel log) {
		return null;
	}
	
	public TelemetryDataEntry [] getBuffers() {
		return null;
	}
}
